<template>
  <a-spin :spinning='confirmLoading'>
    <j-form-container :disabled='formDisabled'>
      <a-form :form='form' slot='detail'>
        <a-row>
          <a-col :span='12'>
            <a-form-item label='申请编号' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['num']" disabled placeholder='系统自动生成'></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='申请人' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['createBy']" disabled />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='申请时间' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-date v-decorator="['createTime']" :showTime='false' dateFormat='YYYY-MM-DD' style='width: 100%' />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='费用所属' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-dict-select-tag type='list' v-decorator="['auditGrade', validatorRules.auditGrade]"
                                 :trigger-change='true' dictCode='audit_grade' placeholder='请选择费用所属' />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='费用类别' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-dict-select-tag type='list' v-decorator="['costType', validatorRules.costType]" :trigger-change='true'
                                 dictCode='cost_type' placeholder='请选择费用类别' @change='typeChange' />
            </a-form-item>
          </a-col>
          <a-col :span='12' v-if="typeDictText==='15'|| typeDictText==='100'||typeDictText==='99'|| model.projectId">
            <a-form-item label='项目名称' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-popup
                placeholder='请点击选择项目名称'
                v-decorator="['projectName',validatorRules.projectName]"
                :trigger-change='true'
                org-fields='id,short_name,customer_name'
                dest-fields='projectId,projectName,shortName'
                code='pm_project_select'
                @callback='projectNamePopupCallback'
              />
              <input v-decorator="['projectId']" v-show='false'></input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='采购名称' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['purchaseName', validatorRules.purchaseName]" placeholder='请输入采购名称'></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='6' v-if=" typeDictText==='100'">
            <a-form-item label='是否核算' :labelCol='labelCol3' :wrapperCol='wrapperCol3'>
              <j-switch v-decorator="['isCheck', validatorRules.isCheck]"
              ></j-switch>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='数量' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input-number :min='0' v-decorator="['count', validatorRules.count]" placeholder='请输入数量'
                              style='width: 100%' />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='金额' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input-number v-decorator="['money', validatorRules.money]" placeholder='请输入金额' style='width: 100%' />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='支付方式' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-dict-select-tag type='list' v-decorator="['payment',  validatorRules.payment]" :trigger-change='true'
                                 dictCode='cost_payment' placeholder='请选择支付方式' />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='收款人' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['leader', validatorRules.leader]" placeholder='请输入收款人'></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='收款银行' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['bankDeposit', validatorRules.bankDeposit]" placeholder='请输入收款银行'></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='收款账号' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['account', validatorRules.account]" placeholder='请输入收款账号'></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='24'>
            <a-form-item label='费用说明' :labelCol='labelCol2' :wrapperCol='wrapperCol2'>
              <a-textarea v-decorator="['costExplain', validatorRules.costExplain]" :autoSize='{minRows :4}'
                          placeholder='请输入费用说明'></a-textarea>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='附件' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-upload v-decorator="['accessories']" :trigger-change='true'></j-upload>
            </a-form-item>
          </a-col>
          <a-col :span='12' v-if='model.bpmStatus == "3"'>
            <a-form-item label='已发放' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-switch v-decorator="['issue']"></j-switch>
            </a-form-item>
          </a-col>
          <a-col v-if='showFlowSubmitButton' :span='24' style='text-align: center'>
            <a-button @click='submitForm'>提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'HfOperationCostApplyForm',
  components: {},
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {
      },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  watch: {
    //流程表单的提交的监听事件，用来监听修改或者操作流程的表单
    // 同时需要修改 submitForm方法

    '$store.state.bpm.bpmSubmitStates': {
      immediate: true,
      handler(val) {
        if (val && val.edit && val.edit === '1') {
          this.submitForm().then(res => {
            if (res) {
              this.$store.commit('BPM_SUBMIT_STATES', {edit: '2'})
            }
          })
        }
      },
    }
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      labelCol3: {
        xs: { span: 12 },
        sm: { span: 12 }
      },
      wrapperCol3: {
        xs: { span: 12 },
        sm: { span: 3 }
      },
      confirmLoading: false,
      validatorRules: {
        projectName: {
          rules: [{ required: true, message: '请选择项目!' }]
        },
        auditGrade: {
          rules: [
            { required: true, message: '请输入费用所属!' }
          ]
        },
        costType: {
          rules: [
            { required: true, message: '请输入费用类别!' }
          ]
        },
        costExplain: {
          rules: [
            { required: true, message: '请输入费用说明!' }
          ]
        },
        isCheck: {
          rules: [
            { required: true, message: '请选择是否核算!' }
          ]
        },
        purchaseName: {
          rules: [
            { required: true, message: '请输入采购名称!' }
          ]
        },
        count: {
          rules: [
            { required: true, message: '请输入数量!' }
          ]
        },
        money: {
          rules: [
            { required: true, message: '请输入金额!' }
          ]
        },
        payment: {
          rules: [
            { required: true, message: '请选择支付方式!' }
          ]
        },
        leader: {
          rules: [
            { required: true, message: '请输入收款人!' }
          ]
        },
        account: {
          rules: [
            { required: true, message: '请输入收款账号!' }
          ]
        },
        bankDeposit: {
          rules: [
            { required: true, message: '请输入收款银行!' }
          ]
        }
      },
      url: {
        add: '/operate/hfOperationCostApply/add',
        edit: '/operate/hfOperationCostApply/edit',
        queryById: '/operate/hfOperationCostApply/queryById'
      },
      fields: [
        'createBy',
        'createTime',
        'num',
        'auditGrade',
        'costType',
        'costExplain',
        'purchaseName',
        'count',
        'money',
        'payment',
        'leader',
        'account',
        'bankDeposit',
        'accessories',
        'bpmStatus',
        'issue',
        'projectId',
        'projectName',
        'isCheck'
      ],
      typeDictText: ''
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  mounted() {
  },
  methods: {
    typeChange(value) {
      this.typeDictText = value
      this.model = {
        ...this.model,
        projectId: '',
        projectName: ''
      }
    },
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, this.fields))
      })
    },
    addRecord(record) {
      this.edit(record)
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    validateError(msg) {
      this.$message.error(msg)
    },
    submitForm() {
      const that = this
      return new Promise(((resolve, reject) => {
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            console.log('表单提交数据', formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                resolve(true)
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
            })
          } else {
            this.$store.commit('BPM_SUBMIT_STATES', {})
          }
        })
      }))
    },
    projectNamePopupCallback(row) {
      this.projectId = row.projectId
      this.form.setFieldsValue(pick(row, this.fields))
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, this.fields))
    }
  }
}
</script>